/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package semant.first_pass;

import semant.Env;
import symbol.MethodInfo;
import symbol.Symbol;
import symbol.VarInfo;
import syntaxtree.Formal;
import syntaxtree.VisitorAdapter;

/**
 *
 * @author Thiago
 */
class FormalHandler extends VisitorAdapter {

    private Env env;
    private MethodInfo method;

    private FormalHandler(Env e, MethodInfo m)
    {
        env = e;

        method = m;
    }

    static void firstPass(Env e, MethodInfo m, Formal node)
    {
        
        FormalHandler h = new FormalHandler(e,m);

        node.accept(h);
    }

    @Override
    public void visit(Formal node)
    {

        Symbol name = Symbol.symbol(node.name.s);

        VarInfo v = new VarInfo(node.type, name);

        if(!method.addFormal(v))
        {
            VarInfo old = method.formalsTable.put(name, v);

            env.err.Error(node.name, new Object[]{"Parâmetro \'"+ name + "\' redeclarado para o método \'" + method.name + "\'",
                                        "Declaração anterior aqui: [" + old.type.line + "," + old.type.row + "]"});
        }
        
    }

}
